﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using BallotAiying2;

namespace 学籍网
{
    public partial class Form_yzm : Form
    {
        static string[] codes = new string[10];
        public Form_yzm()
        {
            InitializeComponent();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            UnCodebase unCode = new UnCodebase((Bitmap)pictureBox1.Image);
            unCode.GetPicValidByValue(128, 4); //得到有效空间
            Bitmap[] pics = unCode.GetSplitPics(4, 1);     //分割
            pictureBox_single1.Image = pics[0];
            pictureBox_single2.Image = pics[1];
            pictureBox_single3.Image = pics[2];
            pictureBox_single4.Image = pics[3];

            UnCodebase[] unCodeArray = new UnCodebase[4];
            for (int i = 0; i < 4; i++)
            {
                unCodeArray[i] = new UnCodebase(pics[i]);
                unCodeArray[i].ClearNoise(unCode.GetDgGrayValue());
                unCodeArray[i].GrayByPixels(); //灰度处理
            }
            pictureBox_single1.Image = unCodeArray[0].bmpobj;// pics[0];
            pictureBox_single2.Image = unCodeArray[1].bmpobj;// pics[1];
            pictureBox_single3.Image = unCodeArray[2].bmpobj;// pics[2];
            pictureBox_single4.Image = unCodeArray[3].bmpobj;// pics[3];
            //string code = unCode.GetSingleBmpCode(pics[i], 128);   //得到代码串
        }

        /// <summary>
        /// 对比
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        private static int Compare(string bytes, int argDiffer=15)
        {
            int differ = 0;//差异量
            for (int i = 0; i < codes.Length; i++)
            {
                for (int j = 0; j < codes[i].Length; j++)
                {
                    if (codes[i][j] == bytes[j])
                        continue;
                    else
                        differ++;
                }
                if (differ < argDiffer)//差异量小于15意味着相似度很高直接取值，15这个值可以根据情况调节
                {
                    return i;
                }
                else
                    differ = 0;
            }
            return 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = "";

            UnCodebase unCode = new UnCodebase((Bitmap)pictureBox_single1.Image);
            string code = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(code);
            textBox2.AppendText(Compare(code,40).ToString());

            unCode = new UnCodebase((Bitmap)pictureBox_single2.Image);
            code = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(code);
            textBox2.AppendText(Compare(code, 40).ToString());

            unCode = new UnCodebase((Bitmap)pictureBox_single3.Image);
            code = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(code);
            textBox2.AppendText(Compare(code, 40).ToString());

            unCode = new UnCodebase((Bitmap)pictureBox_single4.Image);
            code = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(code);
            textBox2.AppendText(Compare(code, 40).ToString());
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UnCodebase unCode = new UnCodebase((Bitmap)pictureBox_single1.Image);
            codes[int.Parse(textBox_single1.Text)] = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(codes[int.Parse(textBox_single1.Text)]);
            unCode = new UnCodebase((Bitmap)pictureBox_single2.Image);
            codes[int.Parse(textBox_single2.Text)] = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(codes[int.Parse(textBox_single2.Text)]);
            unCode = new UnCodebase((Bitmap)pictureBox_single3.Image);
            codes[int.Parse(textBox_single3.Text)] = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(codes[int.Parse(textBox_single3.Text)]);
            unCode = new UnCodebase((Bitmap)pictureBox_single4.Image);
            codes[int.Parse(textBox_single4.Text)] = unCode.GetSingleBmpCode(unCode.bmpobj, 128);   //得到代码串
            System.Diagnostics.Debug.WriteLine(codes[int.Parse(textBox_single4.Text)]);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(folderBrowserDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath + "\\";
                foreach (string file in Directory.GetFiles(textBox1.Text, "*.jpg"))
                {
                    listBox1.Items.Add(file.Replace(textBox1.Text, ""));
                }
                foreach (string file in Directory.GetFiles(textBox1.Text, "*.jpeg"))
                {
                    listBox1.Items.Add(file.Replace(textBox1.Text, ""));
                }
            }
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine((int)e.KeyChar);
            if(e.KeyChar == 13)
            {
                if (!textBox1.Text.EndsWith("\\")) textBox1.Text = textBox1.Text + "\\";
                foreach (string file in Directory.GetFiles(textBox1.Text, "*.jpg" ))
                {
                    listBox1.Items.Add(file.Replace(textBox1.Text, ""));
                }
                foreach (string file in Directory.GetFiles(textBox1.Text, "*.jpeg"))
                {
                    listBox1.Items.Add(file.Replace(textBox1.Text, ""));
                }
            }
        }

        /// <summary>
        /// 枚举文件夹内文件(含子文件夹) 
        /// </summary>
        /// <param name="DirPath"></param>
        /// <param name="searchPattern"></param>
        /// <param name="LI_Files"></param>
        void GetAllFileByDir(string DirPath, string searchPattern, List<string> LI_Files)
        {
            //列举出所有文件,添加到AL
            foreach (string file in Directory.GetFiles(DirPath, searchPattern))
                LI_Files.Add(file);
            //列举出所有子文件夹,并对之调用GetAllFileByDir自己;
            foreach (string dir in Directory.GetDirectories(DirPath))
                GetAllFileByDir(dir, searchPattern, LI_Files);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex >= 0)
            {
                pictureBox1.ImageLocation = textBox1.Text + listBox1.SelectedItem.ToString();
            }
        }
    }
}
